<?php

/**
 * Controls the functionality of web surveys provided through the presentation layer (view)
 * to the user
 * The actions were created based on the tutorial of CakePHP Cookbook
 * http://book.cakephp.org/2.0/_downloads/en/CakePHPCookbook.pdf
 * 
 * PHP5
 * 
 * @package Controller.WebSurveysController
 */
class WebSurveysController extends AppController {

    public $helpers = array('Html', 'Form');
    public $components = array('Session');
    var $websurveys;

    public function index() {
        $websurveys = $this->WebSurvey->find('all',
                                            array('conditions' => 
                                                        array('surveyor_id' => '1')));
        $this->set('websurveys', $websurveys);
    }

    /**
     * Action to edit a websurvey, 
     * @param type $id 
     * @param type $questionPos
     * @throws NotFoundException
     */
    public function edit($id = null, $questionPos = '0') {
        if (!$id) {
            throw new NotFoundException(__('Invalid survey'));
        }
        $websurvey = $this->WebSurvey->findById($id);
        if (!$websurvey) { 
            throw new NotFoundException(__('Invalid survey'));
        }
        $this->set('websurvey', $websurvey);
        $this->set('questionPos', $questionPos);
        
    }

    /**
     * Create survey action
     * @return Redirection to edit survey page
     */
    public function create() {
        if ($this->request->isPost()) {
            $this->WebSurvey->create();
            $this->request->data['Surveyor']['id'] = '1';
            debug('surveyor id ' . $this->request->data['Surveyor']['id']);
            if ($this->WebSurvey->saveAll($this->request->data)) {
                return $this->redirect(array('action' => 'edit', $this->WebSurvey->id, '-1'));
            }
            
        }
     //   return $this->redirect(array('action' => 'index'));
    }
    
    /**
     * Create survey action
     * @return Redirection to edit survey page
     */
    public function createTemplate() {
        if ($this->request->isPost()) {
            $this->WebSurvey->create();
            $this->request->data['Surveyor']['id'] = '2';
            debug('surveyor id ' . $this->request->data['Surveyor']['id']);
            if ($this->WebSurvey->saveAll($this->request->data)) {
                return $this->redirect(array('controller' => 'Templates', 'action' => 'create', $this->WebSurvey->id));
            }
            
        }
    }
}
